package com.cbs.android.extension.webviewclient.sample;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.cbs.android.component.log.L;
import com.cbs.android.extension.webviewclient.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    private TextView urlView;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        urlView = (TextView) findViewById(R.id.url);
        webView = (WebView) findViewById(R.id.webview);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webView.setWebContentsDebuggingEnabled(true);
        }

        urlView.setText("http://service.guguxinge.com:8080/pages/news/0651120a57db0d5675c77c2b784adaf9");

        webView.setWebViewClient(new MyClient());
        webView.getSettings().setJavaScriptEnabled(true);
    }

    public void load(View view) {
        webView.loadUrl(urlView.getText().toString());
    }

    public void log(View view) {
        Intent intent = new Intent(this, LogActivity.class);
        startActivity(intent);
    }

    private class MyClient extends WebViewClient {
        @Override
        public void onCBSPageStarted(WebView view, String url, Bitmap favicon) {
            super.onCBSPageStarted(view, url, favicon);
            CBSLog.logs.add("cbsPageStarted: " + url);
            L.e(TAG, "cbsPageStarted: " + url);
        }

        @Override
        public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            CBSLog.logs.add("pageStarted: " + url);
            L.e(TAG, "pageStarted: " + url);
        }

        @Override
        public void onLoadResource(android.webkit.WebView view, String url) {
            super.onLoadResource(view, url);
            CBSLog.logs.add("loadResource: " + url);
            L.e(TAG, "loadResourced: " + url);
        }
    }
}
